package handler

import (
	"gitee.com/bjf-fhe/apicat/entry"
	"gitee.com/bjf-fhe/apicat/entry/etype"
)

type Config struct {
	MinLevel int
	MinCount int
}

type Judgement struct {
	cacheMap map[string]*entry.LogEntry
	cfg      *Config
}

func (j *Judgement) Judge(en *entry.LogEntry) bool {
	if en.Legal {
		return false
	}
	if old, ok := j.cacheMap[en.Client]; ok {
		en.Count = old.Count + 1
	} else {
		en.Count = 1
	}
	j.cacheMap[en.Client] = en
	return en.Count >= int64(j.cfg.MinCount) && en.ErrorType >= etype.EntryError(j.cfg.MinLevel)
}

func NewJudgement(cfg *Config) *Judgement {
	return &Judgement{
		cacheMap: make(map[string]*entry.LogEntry),
		cfg:      cfg,
	}
}
